package io.renren.modules.manage.controller;

import java.util.Arrays;
import java.util.Map;


import io.renren.modules.manage.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.manage.entity.BannerEntity;
import io.renren.modules.manage.service.BannerService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author hyc
 * @email 2250242464qq.com
 * @date 2024-03-08 23:09:55
 */
@RestController
@RequestMapping("manage/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private UploadService uploadService;
    @Value("${uploads.path}")
    private String uploadDir;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("manage:banner:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bannerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("manage:banner:info")
    public R info(@PathVariable("id") Integer id){
		BannerEntity banner = bannerService.getById(id);

        return R.ok().put("banner", banner);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("manage:banner:save")
    public R save(@RequestBody BannerEntity banner){
		bannerService.save(banner);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("manage:banner:update")
    public R update(@RequestBody BannerEntity banner){
		bannerService.updateById(banner);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("manage:banner:delete")
    public R delete(@RequestBody Integer[] ids){
        String type = "banner";
        Arrays.asList(ids).forEach(id -> {
            BannerEntity banner = bannerService.getById(id);
            String[] split = banner.getUrl().split(UploadController.SEPARATOR);
            uploadService.deleteFile(uploadDir, type, split[2]);
        });
		bannerService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
